package handler

import (
	"adm-mysql/internal/db"
	"github.com/gin-gonic/gin"
	"github.com/jackc/pgx/v5/pgtype"
	"net/http"
	"time"
)

//	CreateDBBackup godoc
//	@Summary		CreateDBBackup
//	@Description	添加备份信息
//	@Tags			DBBackup
//	@Param			param	body	db.CreateDbBackupParams	true	"添加数据库备份信息"
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	db.DbInstance
//	@Router			/database/backup/ [post]
//
// CreateDBBackup 添加备份信息
func CreateDBBackup(c *gin.Context) {
	var param db.CreateDbBackupParams
	if err := c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusBadRequest, err)
		return
	}
	result, err := db.NewStore().CreateDbBackup(c.Request.Context(), param)
	if err != nil {
		c.JSON(http.StatusInternalServerError, err)
		return
	}
	c.JSON(http.StatusOK, result)
}

//	DeleteDBBackup godoc
//	@Summary		DeleteDBBackup
//	@Description	删除数据库备份信息
//	@Tags			DBBackup
//	@Param			param	body	db.DeleteDBBackupParams	true	"删除数据库备份信息"
//	@Accept			json
//	@Produce		json
//	@Success		200	{string}	string	"删除成功"
//	@Router			/database/backup/ [delete]
//
// DeleteDBBackup 删除数据库备份信息
func DeleteDBBackup(c *gin.Context) {
	var param db.DeleteDBBackupParams
	if err := c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusBadRequest, err.Error())
		return
	}
	err := db.NewStore().DeleteDBBackup(c.Request.Context(), param)
	if err != nil {
		c.JSON(http.StatusInternalServerError, err)
		return
	}
	c.JSON(http.StatusOK, "删除成功")
}

//	UpdateDBBackup godoc
//	@Summary		UpdateDBBackup
//	@Description	修改数据库备份信息
//	@Tags			DBBackup
//	@Param			param	body	db.UpdateDBBackupParams	true	"更新参数"
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	db.DbInstance
//	@Router			/database/backup/ [put]
//
// UpdateDBBackup 修改数据库备份信息
func UpdateDBBackup(c *gin.Context) {
	var param db.UpdateDBBackupParams
	if err := c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusBadRequest, err.Error())
		return
	}
	result, err := db.NewStore().UpdateDBBackup(c.Request.Context(), param)
	if err != nil {
		c.JSON(http.StatusInternalServerError, err)
		return
	}
	c.JSON(http.StatusOK, result)
}

// ListDBBackups godoc
//
//	@Summary		ListDBBackup
//	@Description	获取数据库备份信息
//	@Tags			DBBackup
//	@Param			start	query	string	true	"开始时间"
//	@Param			end		query	string	true	"结束时间"
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	[]db.DbBackup
//	@Router			/database/backups/ [get]
//
// ListDBBackups 获取数据库备份信息
func ListDBBackups(c *gin.Context) {
	start, err := time.Parse("2006-01-02", c.Query("start"))
	if err != nil {
		c.JSON(http.StatusBadRequest, err.Error())
		return
	}
	end, err := time.Parse("2006-01-02", c.Query("end"))
	if err != nil {
		c.JSON(http.StatusBadRequest, err.Error())
		return
	}
	result, err := db.NewStore().ListDBBackup(c.Request.Context(), db.ListDBBackupParams{
		Day:   pgtype.Date{Time: start, Valid: true},
		Day_2: pgtype.Date{Time: end, Valid: true},
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, err.Error())
		return
	}
	c.JSON(http.StatusOK, result)
}
